﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ERP_RORZE_DLL.Product.DAO;
using ERP_RORZE_DLL.Product.database;
using ERP_RORZE_DLL.Product;

namespace ERP_RORZE_PRODUCT.Product.MO
{
    public partial class ctrlMOQtyChange : DevExpress.XtraEditors.XtraUserControl
    {
        ClsBusOderTable boder = new ClsBusOderTable();
        ClsMODAO clsMODAO = new ClsMODAO();
        ClsMOOptionTable MOO = new ClsMOOptionTable();
        ClsMOTable MO = new ClsMOTable();
        DataTable dt;
        private string i;

        public ctrlMOQtyChange()
        {
            InitializeComponent();
        }

        private void ctrlMOQtyChange_Load(object sender, EventArgs e)
        {
            dt = clsMODAO.GetMOQtyChange();
            List<ClsMOTable> objs = new List<ClsMOTable>();
            foreach (DataRow r in dt.Rows)
            {
                ClsMOTable obj = new ClsMOTable();
                obj.MONo = r["MONo"].ToString();  //
                obj.Date = (DateTime)r["Deadline"];
                obj.PartID = r["PartID"].ToString();     ////
                obj.MOQty = (int)r["MOQty"];////
                obj.Finished = bool.Parse(r["Finished"].ToString());
                objs.Add(obj);
            }
            cbMONo.DataSource = objs;
            cbMONo.DisplayMember = "MONo";
        }

        private void btnChange_Click(object sender, EventArgs e)
        {
            if (txtNewQty.Text == "")
            {
                MessageBox.Show("Chưa nhập MOQty mới");
            }
            else
            {
                try
                {
                    int.Parse(txtNewQty.Text);
                    if (int.Parse(txtNewQty.Text) == int.Parse(txtOldQty.Text))
                    {
                        MessageBox.Show("Xem lại đi. MOQty mới = MOQty cũ");
                    }
                    else if (checkFinished.Checked == true)
                    {
                        MessageBox.Show("Đơn hàng đã kết thúc, không thêt thay đổi");
                    }
                    else
                    {
                        //
                        boder.MONo = cbMONo.Text;
                        boder.ChangeDate = dateDate.Value;
                        boder.MOQty = int.Parse(txtNewQty.Text);
                        clsMODAO.UpdateBOderMOQtyChange(boder);

                        //
                        MO.MOQty = int.Parse(txtNewQty.Text);
                        MO.MONo = cbMONo.Text;
                        clsMODAO.UpdateMONoMOQtyChange(MO);

                        //
                        MOO.MOQty = int.Parse(txtNewQty.Text);
                        MOO.MONo = cbMONo.Text;
                        if (int.Parse(txtNewQty.Text) > int.Parse(txtOldQty.Text))
                        {
                            clsMODAO.UpdateMOOpMOQtyChange(MOO);
                        }
                        else
                        {
                            clsMODAO.UpdateMOOpMOQtyChange2(MOO);
                        }
                    }
                }
                catch
                {
                    MessageBox.Show("MOQty mới phải là số nguyên");
                }
            }
        }

        private void cbMONo_SelectedIndexChanged(object sender, EventArgs e)
        {
            ClsMOTable obj = ((ClsMOTable)cbMONo.SelectedItem);
            txtOldQty.Text = obj.MOQty.ToString();
            txtPartNo.Text = obj.PartID.ToString();
            dateDate.Value = DateTime.Parse(obj.Date.ToString());
            checkFinished.Checked = bool.Parse(obj.Finished.ToString());
        }
    }
}
